package com.wyq.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wyq.service.AdminUserService;

public class AdminUserManagerAction extends ActionSupport {
	private AdminUserService adminUserService;
	private List adminUserList;
	private String formStr ;
	
	private String repassword;

	//获取管理员信息
	public String getAdminUserInfo() throws Exception {
		
		//从session中得到当前登录的用户的信息（）
		Map session = ActionContext.getContext().getSession();
		adminUserList = (List) session.get("adminUserList");
		System.out.println("session:::" + adminUserList);
		
		return SUCCESS;
		
	}
	
	//新增或修改
	public String saveAdminUserInfo(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("application/json; charset=utf-8");
			PrintWriter out=response.getWriter();
			try {
				formStr = java.net.URLDecoder.decode(formStr,"UTF-8");
				Map adminUserMap = (Map)JSONUtil.deserialize(formStr);
				
				if(null != repassword && !"".equals(repassword) 
						&& !adminUserMap.get("password").toString().equals(repassword)){
					out.print("{success:true,data:'密码不一致，请重新输入！'}");
				}else{
					int flag = adminUserService.updateAdminUser(adminUserMap);
					out.print("{success:true,data:'保存成功'}");
				}
				//saveFlag = "{success:true,data:'1'}";
			} catch (Exception e) {
				e.printStackTrace(); 
				out.print("{success:false,reason:'保存失败'}");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public void setAdminUserService(AdminUserService adminUserService) {
		this.adminUserService = adminUserService;
	}

	public List getAdminUserList() {
		return adminUserList;
	}

	public void setFormStr(String formStr) {
		this.formStr = formStr;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	
	

}
